package com.example.jack.community.persenter.login;

import android.os.Handler;
import android.text.TextUtils;
import android.util.Log;

import com.example.jack.community.data.loginbean.LoginStatus;
import com.example.jack.community.data.loginbean.User;
import com.example.jack.community.model.login.LoginView;
import com.example.jack.community.view.login.util.Md5;
import com.google.gson.Gson;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import okhttp3.Call;

/**
 * 登录
 * Created by Tulin on 2017/10/18.
 */

public class LoginPresenterImp implements LoginPresenter {

    //登录View接口
    private LoginView mLoginView;

    //Log
    private static final String TAG = "TonJie";

    //用户延时请求的Handler
    private Handler handler = new Handler();

    //Md5加密
    Md5 md5 = new Md5();

    /**
     * 登录判断
     *
     * @param user
     */
    @Override
    public void login(final User user) {
        /**
         * 延时等待假象
         */
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                //两秒后才执行到这里
                //执行在主线程中
                if (!TextUtils.isEmpty(user.getPhonenum()) //如果手机号码部位空
                        && !TextUtils.isEmpty(user.getPassword())//如果用户密码不为空
                        ) {
                    Test(user);//测试账号,用户名:aaa,密码:123
                } else {
                    mLoginView.showToast("用户名和密码不能为空");//用户名或者密码为空
                }
            }
        }, 600);

    }

    /**
     * 测试账号,用户名:aaa,密码:123
     *
     * @param user 用户对象
     */
    private void Test(User user) {
        if (user.getPassword().equals("123")  //如果密码等于123
                && user.getPhonenum().equals("aaa"))   //手机号码等于aaa
        {
            user.setTocken("测试");
            mLoginView.Share(user);
            mLoginView.loginSuccess("登录成功");  //提示登录成功
        } else {
            okhttp_util(user);  //进行联网请求
        }
    }

    /**
     * 联网请求
     *
     * @param user 用户对象
     */
    public void okhttp_util(final User user) {
        /*
          进行密码的Md5加密
         */
        String password = md5.md5(user.getPassword());

        //进行联网请求
        OkHttpUtils
                .post()
                .addParams("username", user.getPhonenum())
                .addParams("password", password)
                .url("http://192.168.1.11/login/")
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        mLoginView.loginFail("服务器出错");
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        Log.d(TAG, "onResponse: " + response);
                        Gson gson = new Gson();
                        LoginStatus shopInfo = gson.fromJson(response, LoginStatus.class);

                        shopInfo.getStatus();

                        user.setTocken(shopInfo.getToken());

                        if (shopInfo.getStatus().equals("0")) {
                            mLoginView.Share(user);
                            mLoginView.loginSuccess("Login successful");

                        }
                        if (shopInfo.getStatus().equals("2")) {
                            mLoginView.loginFail("User name does not exist");
                        }
                        if (shopInfo.getStatus().equals("1")) {
                            mLoginView.loginFail("I'm sorry for the password error");
                        }
                    }
                });
    }

    @Override
    public void attachView(LoginView v) {
        this.mLoginView = v;
    }

    @Override
    public void detachView() {
        mLoginView = null;
    }

}
